import {  getChannelStatistics ,
  deleteChannel,
  getDayChannelStatisticsList,
  getSystemChannelList,
  onLineChannel,
  insertChannel,
  getTotalChannelStatistics,
} from '../../services/channel/api';


export default {
  namespace: 'channelStatistics',

  state: {

    loading: true,

    channelData:{
      list:[],
      pagination: {},
    },
    applyData:{
      list:[],
      pagination: {},
    }
  },

  effects: {

    *fetchGetChannelStatistics({ payload }, { call, put }) {
      const response = yield call(getChannelStatistics, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            channelData: {
              list: response.data.records,
              pagination:{
                current: response.data.current,
                total: response.data.total,
              }
            }
          },
        });
      }
    },
    *fetchGetTotalChannelStatistics({ payload }, { call, put }) {
      const response = yield call(getTotalChannelStatistics, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            allChannelData: response.data
          },
        });
      }
    },


    *fetchDeleteChannel({ payload  ,callback}, { call, put }) {
      const response = yield call(deleteChannel, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {outData: response},
        });
        if (callback) callback();
      }
    },
    *fetchDayChannel({ payload }, { call, put }) {
      const response = yield call(getDayChannelStatisticsList, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {dayChannelData: response.data},
        });
      }
    },

    *fetchGetSystemChannelList({ payload }, { call, put }) {
      const response = yield call(getSystemChannelList, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            applyData: {
              list: response.data.records,
              pagination:{
                current: response.data.current,
                total: response.data.total,
              }
            }
          },
        });
      }
    },

    *fetchOnLineChannel({ payload  ,callback}, { call, put }) {
      const response = yield call(onLineChannel, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {onlineData: response},
        });
        if (callback) callback();
      }
    },
    *fetchInsertChannel({ payload  ,callback}, { call, put }) {
      const response = yield call(insertChannel, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {addData: response},
        });
      }
      if (callback && response) callback(response);
    },



  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },

};
